<?php

namespace App\Console\Commands;

use App\Http\Models\Mysql\SsyStyleImagesTable;
use App\Http\Models\Mysql\StyleTable;
use App\Http\Repository\CommonRepository;
use App\Http\Repository\SzdptechRepository;
use Carbon\Carbon;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Cache;

class downloadImg extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'command:downloadImg {--all=} {--number=}';

    /**
     * The console command description.
     * 0 0,12 * * *
     * @var string
     */
    protected $description = '定时拉取数加设计档案图片';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    public function handle()
    {
        $autoKey = "auto_downloadImg";
        Cache::put($autoKey, 1, Carbon::now()->addMinutes(15));
        $all = (Int)$this->option('all') ?: 0;
        $number = $this->option('number') ?: "";
        $styleList = [];
        if(!empty($number)){
            //支持个别款号的图片下载
            $numbers = explode(",", $number);
            foreach ($numbers as $number){
                $styleList[] = ["number" => $number];
            }
        }else{
            $conn = (new StyleTable())->select(['number']);
            if(!$all){
                //获取最新处理过的图片时间
                $imageRes = (new SsyStyleImagesTable())->orderBy("createtime", 'desc')->first(['createtime'])->toArray();
                $time = strtotime(date('Y-m-01 00:00:00', strtotime($imageRes['createtime']))) - 1512460800;
                $conn = $conn->where('creation_time', '>=', $time);
            }
            $styleList = $conn->get()->toArray();
        }
        if(empty($styleList)){
            die("没有数据需要处理\n");
            Cache::forget($autoKey);
        }
        $splitNum = 100;
        $total = count($styleList);
        $currentNum = 1;
        for($i = 0; $i < $total; $i++){
            if(empty($styleList)){
                break;
            }
            $numberList = array_splice($styleList, 0, $splitNum);
            $numbers = array_column($numberList, 'number');
            $res = SzdptechRepository::searchStyleImgs($numbers);
            $list = $res['value'] ?? [];
            foreach($list as $item){
                $typeNo = $item['number'];
                //$thumbImgList = [];
                $imgList = [];
                echo "共{$total}条款号, 正执行第{$currentNum}条：{$typeNo}\n";
                $currentNum++;
                foreach($item['img_list'] as $index => $img){
                    //上传原图
                    $imgRes = CommonRepository::uploadCDN($img, 'style', $typeNo."_{$index}");
                    if($imgRes['code'] === 0){
                        $imgList[] = $imgRes['file'];
                    }
                    //上传缩略图
                    //$thumbImgRes = CommonRepository::thumbImg($img, 'style', $typeNo."_s_{$index}");
                    //$thumbImgList[] = $thumbImgRes['file'] ?? ($imgList[0] ?? "");
                }
                if(empty($imgList)){
                    continue;
                }
                $data = [
                    "images" => $imgList[0] ,
                    "thumb_images" => $imgList[0],
                    "images_list" => json_encode($imgList, JSON_UNESCAPED_UNICODE),
                    "thumb_images_list" => json_encode($imgList, JSON_UNESCAPED_UNICODE),
                    "type_no" => $typeNo,
                    "createtime" =>date('Y-m-d H:i:s')
                ];
                $exist = (new SsyStyleImagesTable())->where('type_no', $typeNo)->first();
                if(!empty($exist)){
                    unset($data['createtime']);
                    (new SsyStyleImagesTable())->updateData($exist->id, $data);
                }else{
                    (new SsyStyleImagesTable())->createData($data);
                }
            }
        }
        Cache::forget($autoKey);
    }
}
